<?php

abstract class McCube_Html_Form_Element extends McCube_Html_Abstact{

    public    $id      = '';
    public    $name    = '';
    public    $label   = '';
    protected $_value  = '';
    protected $_model  = null;
    protected $_config = array();
    protected $_errors = array();

    /** */
    abstract protected function _build();

    public function render(){
        if(!isset($this->_config['decorator'])){
            $this->_config['decorator'] = 'Inline';
        }

        $decorator = 'McCube_Html_Form_Decorator_'.$this->_config['decorator'];
        return $decorator::render($this->label, $this->_build());
    }

    /** */
    public function __construct($name, $config=array()){
        $this->name = $name;
        $this->id = str_replace(array('[',']') ,array('_',''), $name);

        if(isset($config['label']))
            $this->label = $config['label'];

        $this->_config  = $config;

        $this ->setAttr('id'  , $this->id);
        $this ->setAttr('name', $name);
        if(isset($config['attrs']))
            $this->setAttrs($config['attrs']);
    }

    /** */
    public function __toString(){
        return $this->render();
    }

    /** */
    public function isValid(){
        if(empty($this->_value))
            $this->_errors[] = 'required';

        $this->_applyFilters();
        if(!$this->_errors)
            return true;

        if(!isset($this->_attrs['class']))
            $this->_attrs['class'] = '';

        $this->_attrs['class'] .= ' error';
        return false;
    }

    /** */
    protected function _applyFilters(){
        foreach($this->_config['filters'] as $filter => $params){
            if(!McCube_String_Filters::$filter($this->_value, $params))
               $this->_errors[] = $filter;
        }
    }

    /** */
    protected function _applyFormats($field, $formats){
        foreach($formats as $format=>$params){
            $this->_value = McCube_String_Formats::$format($this->_value, $params);
        }
    }

    /** */
    public function setModel($model){

        $this->_model = $model;
        return $this;
    }

    /** */
    public function getDatasOnDb($tblConfig=null){
        if(!$tblConfig){
            if(isset($this->_config['datas']['db']))
                $tblConfig = $this->_config['datas']['db'];
            else
                return array();
        }

        $fieldForValue = isset($tblConfig['fieldValue'])
                ? $tblConfig['fieldValue']
                : 'name'
        ;

        $fieldForKey = isset($tblConfig['fieldKey'])
                ? $tblConfig['fieldKey']
                : 'id'
        ;

        try{
            return $this->_model->getDatasByPair(
                $tblConfig['table'], $fieldForValue, $fieldForKey
            );
        } catch(Exception $exception){
            McCube_Notifier::render('[@error@Exception@]', $exception->getMessage(), $exception->getTrace());
            exit;
        }
    }

    public function hide(){
        $this ->setAttr('style', 'display:none');
        return $this;
    }

    public function getValue(){
        return $this->_value;
    }

    /** */
    public function setValue($value){
        $this->_value = $value;
        return $this;
    }
}